import axios from "axios"

axios.defaults.baseURL = 'http://mockjs.com/api'
export default function(vm){
    //请求拦截
    axios.interceptors.request.use(config=>{
        //获取token
        const token=localStorage.getItem('token')
        if(token){
            config.headers.Authorization="Bearer "+token
        }
        return config
    })
   //响应拦截
   axios.interceptors.response.use(null,err=>{
       if(err.response.status===401){  // 没有登录或者令牌过期
        // 清空vuex和localstorage  
        vm.$store.dispatch("logout")
        // 跳转login  
        vm.$router.push("/login");
       }
       return Promise.reject(err);
   })
}
